<template>
	<view class="mainContent">
		<view class="twoSecond">
			<view class="left">					
				<p class="flex">就医时间：
					<picker mode="date" :start="start" :end="end"  :disabled="status == 2"  @change="bindDateChange">
						<view class="date">{{medicalDate || '选择就医时间'}}</view>
					</picker> 
				</p>
				<view><text>就医机构：</text><input type="text" v-model="medicalInstitutions" :disabled="status == 2"></view>
						
			</view>
			<view class="right" v-if="status != 2">
				<view class="save" @click="seveAdd(1)">保存</view>
				<view class="saveSure" @click="seveAdd(2)">确认存档</view>
			</view>
		</view>
		<view class="uploadBox">
			<u-upload  ref="uUpload" :header="header" :form-data="{type:12}" :action="action" :file-list="imgUrls" :deletable="status != 2" @on-uploaded="uploaded" @on-remove="remove"></u-upload>
		</view>
		<u-modal v-model="showModal" :show-cancel-button="true" @confirm="Modalconfirm" :content="showContent"></u-modal>
	</view>
</template>

<script>
	import http from '@/common/http/interface.js'
	import {getUrlParam} from "@/static/js/filter.js"
	export default {
		data () {
			const currentDate = this.getDate({
				format: true
			})
			return {
				start: '',
				end: currentDate,
				action: http.config.baseUrl+'/File/multipleFiles',
				header:{
					"Token": localStorage.token
				},
				status:'', 
				showModal:false,
				showContent:'',
				medicalInstitutions: '',
				medicalDate: '',
				dropDownshow:false,	
				imgUrls: [],
				urls: [],
				id: ''
			}
		},
		onLoad() {
			this.id = getUrlParam('id');
			if(this.id) {
				this.$api.AppMedicalRecordsInfo(this.id).then((res) => {
					if(res.data.returnCode == '200') {
						this.medicalDate = res.data.result && res.data.result.medicalDate
						this.medicalInstitutions = res.data.result && res.data.result.medicalInstitutions
						this.status = res.data.result && res.data.result.status
						this.imgUrls = res.data.result && res.data.result.urlList.map((a)=>{
							return {url:a}
						})
					}
					
				})
			}
		},
		methods: {
			remove(index, lists, name){
				this.imgUrls = lists
		    },
			uploaded(lists, name){
				this.imgUrls = lists.map((a)=>{
					if(a.file){
						a.url = a.response.result[0]
					}
					return a
				})
			},
			seveAdd(e) {
				this.urls = this.imgUrls.map((a)=>{
					if(a.response){
						return a.response.result[0]
					}else{
						return a.url
					}
				}) 	
				const AppDrugsRecordReq = {	
					medicalInstitutions: this.medicalInstitutions,
					medicalDate: this.medicalDate,
					urls: this.urls,
					status: e,
				}
				if(!this.medicalDate){
					uni.showToast({
						title:'请填写时间！',
						icon:'none'
					})
					return;
				}
				if(!this.medicalInstitutions){
					uni.showToast({
						title:'请输入就医机构！',
						icon:'none'
					})
					return;
				}
				if(this.imgUrls.length <= 0){
					uni.showToast({
						title:'至少上传一张图片！',
						icon:'none'
					})
					return;
				}
				
				if(this.id) {
					if(e == 1) {
						AppDrugsRecordReq.id = Number(this.id);
						this.$api.AppMedicalRecordsUpdate(this.id,AppDrugsRecordReq).then((res) => {
							if(res.data.returnCode == '200') {
								uni.showToast({
									title: '编辑成功',
									icon: 'none'
								})
								setTimeout(() => {
									uni.navigateTo({
										url:'/pages/monitor/medicalRecord/list'
									})
								},1000)
							}
						})
					} else {
						this.showModal = true,
						this.showContent = '确认信息无误，存档后不可修改。',
						this.sureConfirm = AppDrugsRecordReq
					}
				}else {
					if(e == 1) {
						this.$api.AppMedicalRecordsSave(AppDrugsRecordReq).then((res) => {
							if(res.data.returnCode == '200') {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								})
								setTimeout(() => {
									uni.navigateTo({
										url:'/pages/monitor/medicalRecord/list'
									})
								},1000)
							}
						})
					} else {
						this.showModal = true,
						this.showContent = '确认信息无误，存档后不可修改。'
						this.sureConfirm = AppDrugsRecordReq
					}
					
				}			
	
			},
			Modalconfirm() {
				if(this.id) {
					this.sureConfirm.id = Number(this.id);
					this.$api.AppMedicalRecordsUpdate(this.id,this.sureConfirm).then((res) => {
						if(res.data.returnCode == '200') {
							uni.showToast({
								title: '存档成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/monitor/medicalRecord/list'
								})
							},1000)
						}
					})
					
				} else {
					this.$api.AppMedicalRecordsSave(this.sureConfirm).then((res) => {
						if(res.data.returnCode == '200') {
							uni.showToast({
								title: '存档成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/monitor/medicalRecord/list'
								})
							},1000)
						}
					})
				}
			},

			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
			// 日期
			bindDateChange(e) {  //日期选择
				this.medicalDate = e.target.value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.mainContent {
		width: 750upx;
		min-height: calc(100vh - 44px);
		background-color: #FFFFFF;
		.twoSecond {
			width: 750upx;
			height: 176upx;		
			background: #F5FFFB;
			// box-shadow: 0upx 4upx 30upx 0upx rgba(222, 222, 222, 0.3);
			display: flex;
			position: fixed;
			top: 44px;
			align-items: center;
			padding: 0 24upx;
			box-sizing: border-box;
			z-index: 200;
			.left {
				flex: 1;
				width: 702upx;
				view {
					display: flex;
					align-items: center;
					text {
						color: #333333;
						font-size: 30upx;
					}				
					input {
						width: 319upx;
						height: 46upx;
						padding-left: 10upx;
						border: 1upx solid #E4E4E4;
						border-radius: 6upx;
						display: inline-block;
						margin: 10upx 0;
					}
				}
			}
			.right {
				view {
					width: 174upx;
					height: 46upx;
					background: #FF8F23;
					border-radius: 8upx;
					color: #FFFFFF;
					font-size: 30upx;
					text-align: center;
					line-height: 46upx;
				}
				.saveSure {
					background-color: #38CB97;
					margin-top: 21upx;
				}
			}
		}
		.uploadBox {
			margin-top: 200rpx;
			width: 750upx;
			padding: 0 60upx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			.u-upload{
				width: 100%;
				display: flex;
				justify-content: space-between;
				/deep/ .u-list-item {
					width: 266upx !important;
					height: 350upx !important;
					background: #EFEFEF;
					border-radius: 10upx;
					position: relative;
				}
			}
		}
	}
	.flex{
		display: flex;
		font-size: 30rpx;
		.date{
			width: 334upx;
			height: 50upx;
			padding-left: 10upx;
			border: 1upx solid #E4E4E4;
			border-radius: 6upx;
		}
	}
</style>

